from selenium.webdriver.support.ui import WebDriverWait

class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver = driver

    # 打开网址
    def open_url(self,url):
        self.driver.get(url)

    # 封装定位元素
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    # 封装点击事件
    def click(self,loc):
        self.find_element(loc).click()

    # 封装输入
    def send_keys(self,loc,text):
        self.find_element(loc).send_keys(text)

    # 封装切窗口事件
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # iframe处理
    def iframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    # 弹框处理 确认
    def alert_accept(self):
        self.driver.switch_to.alert.accept()
    # 取消
    def alert_dismiss(self):
        self.driver.switch_to.alert.dismiss()

    # 滚动条
    def scroll_To(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')







